package org.example.rabbitmq.client.concurrent;

import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.SmartLifecycle;

import java.util.ArrayList;
import java.util.List;

public class DynamicMessageListenerContainerLifecycle implements SmartLifecycle {
    boolean isRunning = false;
    final List<SimpleMessageListenerContainer> containers = new ArrayList<>();

    public void addContainer(SimpleMessageListenerContainer container) {
        containers.add(container);
    }

    @Override
    public void start() {
        for (SimpleMessageListenerContainer container : containers) {
            container.start();
        }
        isRunning = true;
    }

    @Override
    public void stop() {
        for (SimpleMessageListenerContainer container : containers) {
            container.stop();
        }
        isRunning = false;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }
}